﻿package src {
	import src.utils.FpsCounterHolder;
	import src.states.*;
	import flash.display.MovieClip;
	import flash.events.Event;
	import src.sound.SoundPlayer;
	import src.game.managers.SoundManager;
	
	public class Game extends MovieClip{
		private var states:GameStatesMachine;
		private var fpsCounter:FpsCounterHolder;
		
		public function Game() {
			trace("konstruktor game");
			//stage.focus = this.stage;
			initStates();
			initFpsCounter();
			initBackgroundMusic();
			initLoop();
		}
		
		public function startGame():void {
			states.setCurrentState(states.getNewLevelState());
		}
		
		private function initStates():void {
			states = new GameStatesMachine(this);
		}
		
		private function initFpsCounter():void {
			fpsCounter = new FpsCounterHolder(this);
			//fpsCounter.show();
		}
		
		private function initLoop():void {
			this.addEventListener(Event.ENTER_FRAME, loop);
		}
		
		private function loop(event:Event):void {
			updateFrameRate();
			states.getCurrentState().update();
		}
		
		private function updateFrameRate():void {
			fpsCounter.update();
		}
		
		private function initBackgroundMusic():void {
			var sm:SoundManager = SoundManager.getInstance();
			var wave:SoundPlayer = new SoundPlayer(new MainTheme() );
			wave.setRepeat(true);
			var theme:SoundPlayer = new SoundPlayer(new IslandFever() );
			theme.setRepeat(true);
			sm.add(wave);
			sm.add(theme);
		}
	}
}